package cn.ococci.dm.Main.Tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import cn.ococci.dm.Base.BaseApp;

/**
 * Created by cyan on 15/9/21.
 * 图像工具类
 */
public class ImageTool {





    /**
     * 把不透明的颜色替换成主题色（并保持透明度）

     * @return
     */
    public static Bitmap replaceColorPix(int resId) {
        Bitmap src = BitmapFactory.decodeResource(BaseApp.getInstance()
                .getResources(), resId);
        return replaceColorPix(src);
    }

    public static Bitmap replaceColorPix(Bitmap src) {
        int width = src.getWidth();
        int height = src.getHeight();
        int[] colors = new int[width * height];
        int tc = BaseApp.COLOR;
        int red = Color.red(tc);
        int green = Color.green(tc);
        int blue = Color.blue(tc);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int color = src.getPixel(i, j);
                int alpha = Color.alpha(color);
                if (alpha > 0) {
                    colors[j * width + i] = Color.argb(alpha, red, green, blue);
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(colors, width, height,
                src.getConfig());
        return bitmap;
    }

    public static Bitmap replaceColorPix(int resId, int color) {
        Bitmap src = BitmapFactory.decodeResource(BaseApp.getInstance()
                .getResources(), resId);
        return replaceColorPixBabyModel(src, color);
    }

    public static Bitmap replaceColorPixBabyModel(Bitmap src, int babyColor) {
        int width = src.getWidth();
        int height = src.getHeight();
        int[] colors = new int[width * height];
        int tc = babyColor;
        int red = Color.red(tc);
        int green = Color.green(tc);
        int blue = Color.blue(tc);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int color = src.getPixel(i, j);
                int alpha = Color.alpha(color);
                if (alpha > 0) {
                    colors[j * width + i] = Color.argb(alpha, red, green, blue);
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(colors, width, height,
                src.getConfig());
        return bitmap;
    }

    public static Bitmap replaceColorPix2(int resId) {
        Bitmap src = BitmapFactory.decodeResource(BaseApp.getInstance()
                .getResources(), resId);
        return replaceColorPix2(src);
    }

    public static Bitmap replaceColorPix2(Bitmap src) {
        int width = src.getWidth();
        int height = src.getHeight();
        int[] colors = new int[width * height];
        int tc = BaseApp.COLOR;
        int red = Color.red(tc);
        int green = Color.green(tc);
        int blue = Color.blue(tc);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int color = src.getPixel(i, j);
                int alpha = 255 - Color.alpha(color);
                colors[j * width + i] = Color.argb(alpha, red, green, blue);
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(colors, width, height,
                src.getConfig());
        return bitmap;
    }

    /**
     * 把非白色的其他颜色替换成主题色
     *
     * @param src
     * @return
     */
    public static Bitmap replaceColorPixExceptWhite(Bitmap src) {
        int width = src.getWidth();
        int height = src.getHeight();
        int[] colors = new int[width * height];
        int tc = BaseApp.COLOR;
        int red = Color.red(tc);
        int green = Color.green(tc);
        int blue = Color.blue(tc);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int color = src.getPixel(i, j);
                if (color == Color.WHITE) {
                    colors[j * width + i] = color;
                    continue;
                }
                int alpha = Color.alpha(color);
                colors[j * width + i] = Color.argb(alpha, red, green, blue);
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(colors, width, height,
                src.getConfig());
        return bitmap;
    }

    public static Bitmap replaceColorPixExceptBlack(Bitmap src) {
        int width = src.getWidth();
        int height = src.getHeight();
        int[] colors = new int[width * height];
        int tc = BaseApp.COLOR;
        int red = Color.red(tc);
        int green = Color.green(tc);
        int blue = Color.blue(tc);
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int color = src.getPixel(i, j);
                if (color == Color.BLACK) {
                    colors[j * width + i] = color;
                    continue;
                }
                int alpha = Color.alpha(color);
                colors[j * width + i] = Color.argb(alpha, red, green, blue);
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(colors, width, height,
                src.getConfig());
        return bitmap;
    }




}
